//
//  YFMomentsCell.m
//  CoollangTennisBall
//
//  Created by Coollang on 2017/9/20.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFMomentsCell.h"
#import "YFMomentsImageView.h"
#import "UIView+JCDrawTool.h"
#import "YFMomentModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "JCLocation.h"
#import "TYAttributedLabel.h"


@interface YFMomentsCell ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *headIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentTextLabel;
@property (weak, nonatomic) IBOutlet UIButton *moreTextButton;// 全文
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *moreTextBtnHeightCon;
@property (weak, nonatomic) IBOutlet YFMomentsImageView *imagesView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imagesHeightCon;

@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *addressLabelHeightCon;

@property (weak, nonatomic) IBOutlet UILabel *postTimeLabel;

/**右上角菜单*/
@property (weak, nonatomic) IBOutlet UIButton *menuBtn;
@property (weak, nonatomic) IBOutlet UIView *likeAndCommentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *likeAndCommentHeightCon;

/*评论和点赞弹框*/
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;
@property (weak, nonatomic) IBOutlet UIButton *likeBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentPopConstraint;
@property (weak, nonatomic) IBOutlet UIView *popCommentView;

/*评论*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentBoardHeightConstraint;
@property (weak, nonatomic) IBOutlet UIView *commentBoardView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentTableviewTopConstraint;
@property (weak, nonatomic) IBOutlet UITableView *commentTableview;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *likeListHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *likeListTopConstraint;
@property (weak, nonatomic) IBOutlet YYLabel *likeListLabel;
@property (weak, nonatomic) IBOutlet UIView *cutLine;

@property (strong, nonatomic) NSTimer *dismissCommentViewTimer;
@property (strong, nonatomic) YFMomentModel *model;
@property (strong, nonatomic) NSIndexPath *indexPath;
@property (weak, nonatomic) id<JYFMomentsCellDelegate> delegate;


@end

@implementation YFMomentsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _popCommentView.layer.cornerRadius = 4;
    _popCommentView.layer.masksToBounds = YES;
    [_popCommentView drawLineWithStartPoint:CGPointMake(0.5*kCommentWidth, 8) endPoint:CGPointMake(0.5*kCommentWidth, 32) lineColor:[UIColor colorWithHex:0xafafaf] lineWidth:0.5];
    _headIconImageView.layer.cornerRadius = 22;
    _headIconImageView.layer.borderWidth = 0.5;
    _headIconImageView.layer.borderColor = [UIColor colorWithHex:0x999999].CGColor;
    
    UITapGestureRecognizer *nickTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nickTapAction:)];
    [_nameLabel addGestureRecognizer:nickTap];
    _commentPopConstraint.constant = 0;
    _commentTableview.delegate = self;
    _commentTableview.dataSource = self;
    [UIView performWithoutAnimation:^{
        [_commentTableview reloadData];
    }];
    
    _likeListLabel.numberOfLines = 0;
    _likeListLabel.displaysAsynchronously = YES;
    _likeListLabel.textAlignment = NSTextAlignmentCenter;
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 点击昵称
 */
- (void)nickTapAction:(UITapGestureRecognizer *)sender {
//    [[NSNotificationCenter defaultCenter] postNotificationName:kNoticeWatchUserInfo object:nil userInfo:@{@"userID":self.model.userID}];
}

/**
 取消所有控件的响应
 */
- (void)cancelAllEdit{
    [_dismissCommentViewTimer invalidate];
    if (_commentPopConstraint.constant == kCommentWidth) {
        _commentPopConstraint.constant = 0;
        [UIView animateWithDuration:.15 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [self layoutIfNeeded];
        } completion:nil];
    }
}
/**
 赋予数据
 @params    model           数据模型
 @params    indexPath       索引
 @params    delegate        代理
 */
- (void)setModel:(YFMomentModel *)model
       indexPath:(NSIndexPath *)indexPath
        delegate:(id<JYFMomentsCellDelegate>)delegate {
    _model = model;
    _indexPath = indexPath;
    _delegate = delegate;
//    model.indexPath = indexPath;
    _nameLabel.text = model.userName;

    [_headIconImageView sd_setImageWithURL:[NSURL URLWithString:model.icon]
                    placeholderImage:[UIImage imageNamed:@"placehold_image"]
                           completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                               
                           }];
    _contentTextLabel.text = model.text;
//    _feature.hidden = !model.isHandpicked;
    _addressLabel.text = model.address;
    _postTimeLabel.text = model.createTime;
    _imagesHeightCon.constant = model.imagesHeight;
    
    if (model.address) {
        _addressLabel.text = model.address;
        _addressLabelHeightCon.constant = 31;
    }else{
        if (model.latitude) {
            __weak typeof(self)weakSelf = self;
            [[JCLocation new] reverseGeocodeLocationWithLatitude:model.latitude
                                                       longitude:model.longitude
                                                        complete:^(NSString *addrssString) {
                                                            model.address = addrssString;
                                                            _addressLabelHeightCon.constant = 31;
                                                            weakSelf.addressLabel.text = addrssString;
                                                        }];
        }else{
             _addressLabelHeightCon.constant = 0;
        }
    }
    
    if (model.isNeedShowMoreBtn) {///显示更多模式
        _moreTextBtnHeightCon.constant = 30;
        _moreTextButton.hidden = NO;
        if (model.showMoreSate == ShowMoreBtnSatePackUp) {///收起
            _contentTextLabel.numberOfLines = 5;
            _moreTextButton.selected = NO;
        }else{///展开
            _contentTextLabel.numberOfLines = 0;
            _moreTextButton.selected = YES;
        }
    }else{
        _moreTextBtnHeightCon.constant = 10;
        _moreTextButton.hidden = YES;
    }
    
    [_imagesView setModelData:model];
    
    _likeBtn.selected = model.isLike;
    _likeListHeightConstraint.constant = model.likeHeight;
    //    _likeListLabel.textContainer = model.likeString;
    //    _likeListLabel.delegate = self;
    _likeListLabel.attributedText = model.likesString;
    ///点赞列表高度及位置
    _likeListHeightConstraint.constant = model.likeHeight;
    if (model.likeList.count == 0) {
        _cutLine.hidden = YES;
        _likeListTopConstraint.constant = 0;
        _commentTableviewTopConstraint.constant = 8;
    }else{
        _cutLine.hidden = NO;
        _likeListTopConstraint.constant = 8;
        _commentTableviewTopConstraint.constant = 0;
    }
    ///评论高度+boardView高度
    _commentBoardHeightConstraint.constant = model.commentHeigh + 8 + model.likeHeight;
//    _commentDataSource.sourceArray = model.responseList;
    [UIView performWithoutAnimation:^{
        [_commentTableview reloadData];
    }];
    if (model.likeHeight == 0 && model.commentHeigh == 0) {
        _commentBoardView.hidden = YES;
    }else{
        _commentBoardView.hidden = NO;
    }
    ///右上角菜单
    _menuBtn.hidden = !model.isMyMoment;
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.model.responseList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

#pragma mark - TYAttributedLabel代理
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)TextRun atPoint:(CGPoint)point {

    if ([TextRun isKindOfClass:[TYLinkTextStorage class]]) {
        id linkStr = ((TYLinkTextStorage*)TextRun).linkData;
        if ([linkStr isKindOfClass:[NSString class]]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:kNoticeWatchUserInfo object:nil userInfo:@{@"userID":linkStr}];
        }
    }
}

@end
